package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.VideoService;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import com.itheima.domain.vo.VideoVo;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class VideoManager {

    @DubboReference
    private VideoService videoService;

    @DubboReference
    private UserInfoService userInfoService;

    //FastDFS客户端
    @Autowired
    private FastFileStorageClient client;

    //webService,用于拼接url
    @Autowired
    private FdfsWebServer webServer;

    //查看视频
    @Cacheable(value = "tanhua" ,key = "'vedio_'+#userId+'_'+#pageNum+'_'+#pageSize")
    public PageBeanVo findVideoByPage(Long userId, Integer pageNum, Integer pageSize) {
        //1、获取pageBeanVo
        PageBeanVo pageBeanVo = videoService.findVideoByPage(userId, pageNum, pageSize);
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();

        List<VideoVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                //视频发布者的id
                UserInfo userInfo = userInfoService.findById(video.getUserId());
                VideoVo vo = new VideoVo();
                vo.setUserInfo(userInfo);
                vo.setVideo(video);
                vos.add(vo);
            }
        }

        pageBeanVo.setItems(vos);
        return pageBeanVo;

    }

    //发布视频
    //@CacheEvict(value = "tanhua" , allEntries = true)
    @CacheEvict(value = "tanhua" , key = "'vedio_'+#userId+'_*'")
    public void saveVideo(Long userId, MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        /*videoThumbnail,视频封面
         videoFile，视频文件*/
        //使用FastDFS分布式文件系统架构发布视频封面
        StorePath storePath = client.uploadFile(videoThumbnail.getInputStream(), videoThumbnail.getSize(), FileUtil.extName(videoThumbnail.getOriginalFilename()),null);
        System.out.println(FileUtil.extName(videoThumbnail.getOriginalFilename()));
        System.out.println(videoThumbnail.getContentType());
        String picUrl = webServer.getWebServerUrl() + storePath.getFullPath();

        //上传视频
        StorePath sp = client.uploadFile(videoFile.getInputStream(), videoFile.getSize(), FileUtil.extName(videoFile.getOriginalFilename()), null);
        String videoUrl = webServer.getWebServerUrl() + sp.getFullPath();

        Video video = new Video();
        video.setCreated(System.currentTimeMillis());
        video.setUserId(userId);
        video.setText("凡哥是无辜的");
        video.setPicUrl(picUrl);
        video.setVideoUrl(videoUrl);

        videoService.saveVideo(video);

    }
}
